package com.wosai.register.controller;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.HttpClientErrorException;

import com.wosai.register.model.BranchExpend;
import com.wosai.register.model.BranchExpendSubject;
import com.wosai.register.model.BranchExpendSubjects;
import com.wosai.register.model.BranchExpendType;
import com.wosai.register.model.BranchExpendTypes;
import com.wosai.register.model.BranchExpends;
import com.wosai.register.model.Categories;
import com.wosai.register.model.Category;
import com.wosai.register.model.Employee;
import com.wosai.register.model.EmployeeAccount;
import com.wosai.register.model.EmployeeAccounts;
import com.wosai.register.model.EmployeeCash;
import com.wosai.register.model.EmployeeCashes;
import com.wosai.register.model.Employees;
import com.wosai.register.model.Order;
import com.wosai.register.model.OrderItem;
import com.wosai.register.model.Orders;
import com.wosai.register.model.Product;
import com.wosai.register.model.Products;
import com.wosai.register.model.Terminal;
import com.wosai.register.security.MyPasswordEncoder;
import com.wosai.register.service.CoreService;
import com.wosai.register.service.QueryService;
import com.wosai.register.service.SecurityService;
import com.wosai.register.util.CollectionUtil;
import com.wosai.register.util.SecurityUtil;

@Controller
public class RestController {
    private static Log logger = LogFactory.getLog(RestController.class);

    private CoreService coreService;
    private QueryService queryService;
    private SecurityService securityService;
    private MyPasswordEncoder passwordEncoder;

    @Autowired
    public RestController(CoreService coreService,
                          QueryService queryService,
                          SecurityService securityService,
                          MyPasswordEncoder passwordEncoder)
    {
        this.coreService = coreService;
        this.queryService = queryService;
        this.securityService = securityService;
        this.passwordEncoder = passwordEncoder;
    }

    @RequestMapping(value="/rest/saveOrders")
    public @ResponseBody Map<String, Object> saveOrders(@RequestBody Orders orders,
                                                        HttpServletRequest request,
                                                        HttpServletResponse response)
    {
        List<Order> ol = orders.getOrders();
        for(Order order: ol){
            order.setMerchantId(orders.getMerchantId());
            order.setBranchId(orders.getBranchId());
            order.setTerminalId(orders.getTerminalId());
        }
        List<String> failed = this.coreService.saveOrders(ol);
        if (failed.isEmpty()){
            return Collections.<String,Object>singletonMap("message", "success");
        }else{
            return CollectionUtil.hashMap("message", "failed to save some of the orders.",
                                          "failedIds", failed);
        }
    }
    
    @RequestMapping(value="/rest/getOrder")
    public @ResponseBody Order getOrder(@RequestParam("mer_id") String merchantId,
                                        @RequestParam("branch_id") String branchId,
                                        @RequestParam("term_id") String terminalId,
                                        @RequestParam("order_id") String orderId,
                                        HttpServletRequest request,
                                        HttpServletResponse response)
    {
        return this.coreService.findOrderById(merchantId, branchId, terminalId, orderId);
    }

    @RequestMapping(value="/rest/getOrders")
    public @ResponseBody List<Order> getOrders(@RequestParam("mer_id") String merchantId,
                                               @RequestParam("branch_id") String branchId,
                                               @RequestParam("term_id") String terminalId,
                                               @RequestParam("page_no") int page,
                                               @RequestParam("page_size") int size,
                                               @RequestParam("date_start") String startDate,
                                               @RequestParam("date_end") String endDate,
                                               HttpServletRequest request,
                                               HttpServletResponse response)
    {
        return this.coreService.findOrdersByTerminal(merchantId, branchId, terminalId, page*size, size, startDate, endDate);
    }

    @RequestMapping(value="/rest/getOrderItems")
    public @ResponseBody List<OrderItem> getOrderItems(@RequestParam("mer_id") String merchantId,
                                                       @RequestParam("branch_id") String branchId,
                                                       @RequestParam("term_id") String terminalId,
                                                       @RequestParam("page_no") int page,
                                                       @RequestParam("page_size") int size,
                                                       HttpServletRequest request,
                                                       HttpServletResponse response)
    {
        return this.coreService.findOrderItemsByTerminal(merchantId, branchId, terminalId, page*size, size);
    }

    @RequestMapping(value="/rest/deleteOrders")
    public @ResponseBody Map<String, Object> deleteOrders(@RequestParam("mer_id") String merchantId,
                                                          @RequestParam("branch_id") String branchId,
                                                          @RequestParam("term_id") String terminalId,
                                                          HttpServletRequest request,
                                                          HttpServletResponse response)
    {
        this.coreService.deleteOrdersByTerminal(merchantId, branchId, terminalId);
        return Collections.<String, Object>singletonMap("message", "success");
    }

    @RequestMapping(value="/rest/deleteOrder")
    public @ResponseBody Map<String, Object> deleteOrder(@RequestParam("mer_id") String merchantId,
                                                         @RequestParam("branch_id") String branchId,
                                                         @RequestParam("term_id") String terminalId,
                                                         @RequestParam("order_id") String orderId,
                                                         HttpServletRequest request,
                                                         HttpServletResponse response)
    {
        this.coreService.deleteOrder(merchantId, branchId, terminalId, orderId);
        return Collections.<String, Object>singletonMap("message", "success");
    }

    @RequestMapping(value="/rest/deleteOrderItems")
    public @ResponseBody Map<String, Object> deleteOrderItems(@RequestParam("mer_id") String merchantId,
                                                              @RequestParam("branch_id") String branchId,
                                                              @RequestParam("term_id") String terminalId,
                                                              @RequestParam("order_id") String orderId,
                                                              HttpServletRequest request,
                                                              HttpServletResponse response)
    {
        this.coreService.deleteOrderItemsByOrder(merchantId, branchId, terminalId, orderId);
        return Collections.<String, Object>singletonMap("message", "success");
    }

    @RequestMapping(value="/rest/saveProducts")
    public @ResponseBody Map<String, Object> saveProducts(@RequestBody Products products,
                                                          HttpServletRequest request,
                                                          HttpServletResponse response)
    {
        List<Product> pl = products.getProducts();
        for(Product product: pl){
            product.setMerchantId(products.getMerchantId());
//            product.setTerminalId(products.getTerminalId());
        }
        List<String> failed = this.coreService.saveProducts(pl);
        if(failed.isEmpty()){
            return Collections.<String,Object>singletonMap("message", "success");
        }else{
            return CollectionUtil.hashMap("message", "failed to save some of the products.",
                                          "failedIds", failed);
        }
    }

    @RequestMapping(value="/rest/getProducts")
    public @ResponseBody List<Product> getProducts(@RequestParam("mer_id") String merchantId,
                                                   HttpServletRequest request,
                                                   HttpServletResponse response)
    {
        return this.coreService.findProductsByTerminal(merchantId);
    }

    @RequestMapping(value="/rest/deleteProducts")
    public @ResponseBody Map<String, Object> deleteProducts(@RequestParam("mer_id") String merchantId,
                                                            HttpServletRequest request,
                                                            HttpServletResponse response)
    {
        this.coreService.deleteProductsByTerminal(merchantId);
        return Collections.<String,Object>singletonMap("message", "success");
    }

    @RequestMapping(value="/rest/saveCategories")
    public @ResponseBody Map<String, Object> saveCategories(@RequestBody Categories categories,
                                                           HttpServletRequest request,
                                                           HttpServletResponse response)
    {
        List<Category> cl = categories.getCategories();
        for(Category category: cl){
            category.setMerchantId(categories.getMerchantId());
//            category.setTerminalId(categories.getTerminalId());
        }
        List<String> failed = this.coreService.saveCategories(cl);
        if (failed.isEmpty()){
            return Collections.<String,Object>singletonMap("message", "success");
        }else{
            return CollectionUtil.hashMap("message", "failed to save some of the categories.",
                                          "failedIds", failed);
        }
    }

    @RequestMapping(value="/rest/getCategories")
    public @ResponseBody List<Category> getCategories(@RequestParam("mer_id") String merchantId,
                                                      HttpServletRequest request,
                                                      HttpServletResponse response)
    {
        return this.coreService.findCategoriesByTerminal(merchantId);
    }

    @RequestMapping(value="/rest/deleteCategories")
    public @ResponseBody Map<String, Object> deleteCategories(@RequestParam("mer_id") String merchantId,
                                                              HttpServletRequest request,
                                                              HttpServletResponse response)
    {
        this.coreService.deleteCategoriesByTerminal(merchantId);
        return Collections.<String,Object>singletonMap("message", "success");
    }

    @RequestMapping(value="/rest/saveEmployees")
    public @ResponseBody Map<String, Object> saveEmployees(@RequestBody Employees employees,
                                                           HttpServletRequest request,
                                                           HttpServletResponse response)
    {
        List<Employee> el = employees.getEmployees();
        for(Employee employee: el){
            employee.setMerchantId(employees.getMerchantId());
            employee.setBranchId(employees.getBranchId());
        }
        List<String> failed = this.coreService.saveEmployees(el);
        
        if (failed.isEmpty()){
            return Collections.<String,Object>singletonMap("message", "success");
        }else{
            return CollectionUtil.hashMap("message", "failed to save some of the employees.",
                                          "failedIds", failed);
        }
    }

    @RequestMapping(value="/rest/getEmployees")
    public @ResponseBody List<Employee> getEmployees(@RequestParam("mer_id") String merchantId,
                                                     HttpServletRequest request,
                                                     HttpServletResponse response)
    {
        return this.coreService.findEmployeesByTerminal(merchantId);
    }

    @RequestMapping(value="/rest/deleteEmployees")
    public @ResponseBody Map<String, Object> deleteEmployees(@RequestParam("mer_id") String merchantId,
                                                             HttpServletRequest request,
                                                             HttpServletResponse response)
    {
        this.coreService.deleteEmployeesByTerminal(merchantId);
        return Collections.<String,Object>singletonMap("message", "success");
    }

    @RequestMapping(value="/rest/saveTerminal")
    public @ResponseBody Map<String, Object> saveTerminals(@RequestBody Terminal terminal,
                                                           HttpServletRequest request,
                                                           HttpServletResponse response)
    {
        this.coreService.saveTerminals(Collections.singletonList(terminal));
        return Collections.<String,Object>singletonMap("message", "success");
    }

    @RequestMapping(value="/rest/getTerminal")
    public @ResponseBody Terminal getTerminal(@RequestParam("mer_id") String merchantId,
                                              @RequestParam("branch_id") String branchId,
                                              @RequestParam("term_id") String terminalId,
                                              HttpServletRequest request,
                                              HttpServletResponse response)
    {
        return this.coreService.findTerminal(merchantId, branchId, terminalId);
    }

    @RequestMapping(value="/rest/deleteTerminal")
    public @ResponseBody Map<String, Object> deleteTerminal(@RequestParam("mer_id") String merchantId,
                                                            @RequestParam("branch_id") String branchId,
                                                            @RequestParam("term_id") String terminalId,
                                                            HttpServletRequest request,
                                                            HttpServletResponse response)
    {
        this.coreService.deleteTerminal(merchantId, branchId, terminalId);
        return Collections.<String,Object>singletonMap("message", "success");
    }

    @RequestMapping(value="/rest/createActivationCode")
    public @ResponseBody Map<String, Object> createActivationCode(@RequestParam("mer_id") String merchantId,
                                                                  @RequestParam("branch_id") String branchId,
                                                                  @RequestParam("term_id") String terminalId,
                                                                  HttpServletRequest request,
                                                                  HttpServletResponse response)
    {
        String code = this.coreService.createActivationCode(merchantId, branchId, terminalId);
        return Collections.<String,Object>singletonMap("active_code", code);
    }

    @RequestMapping(value="/rest/doActivation")
    public @ResponseBody Map<String, Object> doActivation(@RequestParam("active_code") String code,
                                                          HttpServletRequest request,
                                                          HttpServletResponse response)
    {
        return this.coreService.doActivation(code);
    }

    @RequestMapping(value="/rest/doneActivation")
    public @ResponseBody Map<String, Object> doneActivation(@RequestParam("active_code") String code,
                                                            HttpServletRequest request,
                                                            HttpServletResponse response)
    {
        this.coreService.doneActivation(code);
        return Collections.<String,Object>singletonMap("message", "success");
    }

    @RequestMapping(value="/rest/findAggregatedSellAndReturn")
    public @ResponseBody Map<String, Object> findAggregatedSellAndReturn(@RequestParam("mer_id") String merchantId,
                                                                         @RequestParam(value="branch_id", defaultValue="%") String branchId,
                                                                         @RequestParam(value="term_id", defaultValue="%") String terminalId,
                                                                         @RequestParam("date_start") String startDate,
                                                                         @RequestParam("date_end") String endDate)
    {
        return queryService.findAggregatedSellAndReturn(merchantId, branchId, terminalId, startDate, endDate);
    }
    
    @RequestMapping(value="/rest/findPayments")
    public @ResponseBody Map<String, Object> findPaymentByType(@RequestParam("mer_id") String merchantId,
                                                               @RequestParam(value="branch_id", defaultValue="%") String branchId,
                                                               @RequestParam(value="term_id", defaultValue="%") String terminalId,
                                                               @RequestParam("date_start") String startDate,
                                                               @RequestParam("date_end") String endDate)
    {
        return queryService.findPaymentByType(merchantId, branchId, terminalId, startDate, endDate);
    }
    
    @RequestMapping(value="/rest/findOrders")
    public @ResponseBody Map<String, Object> findOrders(@RequestParam("mer_id") String merchantId,
                                                        @RequestParam(value="branch_id", defaultValue="%") String branchId,
                                                        @RequestParam(value="term_id", defaultValue="%") String terminalId,
                                                        @RequestParam("page_no") int page_no,
                                                        @RequestParam("page_size") int page_size,
                                                        @RequestParam("date_start") String startDate,
                                                        @RequestParam("date_end") String endDate)
    {
        return queryService.findOrders(merchantId, branchId, terminalId, page_no*page_size, page_size, startDate, endDate);
    }
    
    @RequestMapping(value="/rest/findReturnedOrders")
    public @ResponseBody Map<String, Object> findReturnedOrders(@RequestParam("mer_id") String merchantId,
                                                                @RequestParam(value="branch_id", defaultValue="%") String branchId,
                                                                @RequestParam(value="term_id", defaultValue="%") String terminalId,
                                                                @RequestParam("page_no") int page_no,
                                                                @RequestParam("page_size") int page_size,
                                                                @RequestParam("date_start") String startDate,
                                                                @RequestParam("date_end") String endDate)
    {
        return queryService.findReturnedOrders(merchantId, branchId, terminalId, page_no*page_size, page_size, startDate, endDate);
    }

    @RequestMapping(value="/rest/findOrderItems")
    public @ResponseBody Map<String, Object> findOrderItems(@RequestParam("mer_id") String merchantId,
                                                            @RequestParam("branch_id") String branchId,
                                                            @RequestParam("term_id") String terminalId,
                                                            @RequestParam("order_id") String orderId)
    {
        return queryService.findOrderItems(merchantId, branchId, terminalId, orderId);
    }

    @RequestMapping(value="/rest/findTop10ByVolume")
    public @ResponseBody Map<String, Object> findTop10ByVolume(@RequestParam("mer_id") String merchantId,
                                                               @RequestParam("date_start") String startDate,
                                                               @RequestParam("date_end") String endDate)
    {
        return queryService.findTop10ByVolume(merchantId, startDate, endDate);
    }

    @RequestMapping(value="/rest/findTop10ByRevenue")
    public @ResponseBody Map<String, Object> findTop10ByRevenue(@RequestParam("mer_id") String merchantId,
                                                                @RequestParam("date_start") String startDate,
                                                                @RequestParam("date_end") String endDate)
    {
        return queryService.findTop10ByRevenue(merchantId, startDate, endDate);
    }

    @RequestMapping(value="/rest/login")
    public @ResponseBody Map<String, Object> login(@RequestParam("username") String username,
                                                   @RequestParam("password") String password,
                                                   HttpServletRequest request)
    {
        String token = securityService.login(request, username, password);
        List<Map<String,Object>> terminals = coreService.findTerminalsByUser(username);
        
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("token", token);
        result.put("roles", terminals);
        return result;
    }

    @RequestMapping(value="/rest/changePassword")
    public @ResponseBody Map<String, Object> changePassword(@RequestParam("username") String username,
                                                            @RequestParam("password") String password,
                                                            @RequestParam("new_password") String newPassword,
                                                            HttpServletRequest request)
    {
        String token = securityService.login(request, username, password);
        
        coreService.changePassword(username, passwordEncoder.encode(newPassword));
        return Collections.<String, Object>singletonMap("message", "success");
    }

    @RequestMapping(value="/rest/getEmployeeCashes")
    public @ResponseBody List<EmployeeCash> getEmployeeCashes(@RequestParam("mer_id") String merchantId,
                                                              @RequestParam("branch_id") String branchId,
                                                              @RequestParam("term_id") String terminalId)
    {
        return this.coreService.findEmployeeCashesByTerminal(merchantId, branchId, terminalId);
    }

    @RequestMapping(value="/rest/saveEmployeeCashes")
    public @ResponseBody Map<String, Object> saveEmployeeCashes(@RequestBody EmployeeCashes employeeCashes)
    {
        List<EmployeeCash> cashes = employeeCashes.getCashes();
        for(EmployeeCash cash: cashes){
            cash.setMerchantId(employeeCashes.getMerchantId());
            cash.setBranchId(employeeCashes.getBranchId());
            cash.setTerminalId(employeeCashes.getTerminalId());
        }
        this.coreService.saveEmployeeCashes(cashes);
        return Collections.<String,Object>singletonMap("message", "success");
    }

    @RequestMapping(value="/rest/getEmployeeAccounts")
    public @ResponseBody List<EmployeeAccount> getEmployeeAccounts(@RequestParam("mer_id") String merchantId,
                                                                   @RequestParam("branch_id") String branchId,
                                                                   @RequestParam("term_id") String terminalId)
    {
        return this.coreService.findEmployeeAccountsByTerminal(merchantId, branchId, terminalId);
    }

    @RequestMapping(value="/rest/saveEmployeeAccounts")
    public @ResponseBody Map<String, Object> saveEmployeeAccounts(@RequestBody EmployeeAccounts employeeAccounts)
    {
        List<EmployeeAccount> accounts = employeeAccounts.getAccounts();
        for(EmployeeAccount account: accounts){
            account.setMerchantId(employeeAccounts.getMerchantId());
            account.setBranchId(employeeAccounts.getBranchId());
            account.setTerminalId(employeeAccounts.getTerminalId());
        }
        this.coreService.saveEmployeeAccounts(accounts);
        return Collections.<String,Object>singletonMap("message", "success");
    }

    @RequestMapping(value="/rest/resetPassword")
    public @ResponseBody Map<String, Object> resetPassword(@RequestParam("username") String username,
                                                           @RequestParam("password") String password)
    {
        try {
            coreService.changePassword(username, passwordEncoder.encode(password));
        }catch(IllegalArgumentException e){
            throw new HttpClientErrorException(HttpStatus.NOT_FOUND, e.getMessage());
        }
        return Collections.<String, Object>singletonMap("message", "success");
    }
    
    @RequestMapping(value="/rest/findTopCategoriesByRevenuePerBranch")
    public @ResponseBody Map<String, Object> findTopCategoriesByRevenuePerBranch(@RequestParam("mer_id") String merchantId,
                                                                                 @RequestParam("branch_id") String branchId,
                                                                                 @RequestParam("date_start") String startDate,
                                                                                 @RequestParam("date_end") String endDate)
    {
        return queryService.topCategoriesByRevenuePerBranch(merchantId, branchId, startDate, endDate);
    }

    @RequestMapping(value="/rest/findTopProductsByRevenuePerBranch")
    public @ResponseBody Map<String, Object> findTopProductsByRevenuePerBranch(@RequestParam("mer_id") String merchantId,
                                                                               @RequestParam("branch_id") String branchId,
                                                                               @RequestParam("date_start") String startDate,
                                                                               @RequestParam("date_end") String endDate)
    {
        return queryService.topProductsByRevenuePerBranch(merchantId, branchId, startDate, endDate);
    }

    @RequestMapping(value="/rest/findCashierShifts")
    public @ResponseBody Map<String, Object> findCashierShifts(@RequestParam("mer_id") String merchantId,
                                                               @RequestParam("branch_id") String branchId,
                                                               @RequestParam("date_start") String startDate,
                                                               @RequestParam("date_end") String endDate)
    {
        return queryService.findShifts(merchantId, branchId, startDate, endDate);
    }

    @RequestMapping(value="/rest/saveBranchExpendTypes")
    public @ResponseBody Map<String, Object> saveBranchExpendTypes(@RequestBody BranchExpendTypes types,
                                                                   HttpServletRequest request,
                                                                   HttpServletResponse response)
    {
        List<BranchExpendType> l = types.getExpendTypes();
        for(BranchExpendType type: l){
            type.setMerchantId(types.getMerchantId());
            type.setBranchId(types.getBranchId());
            type.setTerminalId(types.getTerminalId());
        }
        List<String> failed = this.coreService.saveBranchExpendTypes(l);
        if (failed.isEmpty()){
            return Collections.<String,Object>singletonMap("message", "success");
        }else{
            return CollectionUtil.hashMap("message", "failed to save some of the expend types.",
                                          "failedIds", failed);
        }
    }
    
    @RequestMapping(value="/rest/getBranchExpendTypes")
    public @ResponseBody List<BranchExpendType> getBranchExpendTypes(@RequestParam("mer_id") String merchantId,
                                                                     @RequestParam(value="branch_id",defaultValue="%") String branchId,
                                                                     @RequestParam("term_id") String terminalId,
                                                                     HttpServletRequest request,
                                                                     HttpServletResponse response)
    {
        return this.coreService.findBranchExpendTypes(merchantId, branchId, terminalId);
    }

    @RequestMapping(value="/rest/saveBranchExpendSubjects")
    public @ResponseBody Map<String, Object> saveBranchExpendSubjects(@RequestBody BranchExpendSubjects subjects,
                                                                      HttpServletRequest request,
                                                                      HttpServletResponse response)
    {
        List<BranchExpendSubject> l = subjects.getExpendSubjects();
        for(BranchExpendSubject type: l){
            type.setMerchantId(subjects.getMerchantId());
            type.setBranchId(subjects.getBranchId());
            type.setTerminalId(subjects.getTerminalId());
        }
        List<String> failed = this.coreService.saveBranchExpendSubjects(l);
        if (failed.isEmpty()){
            return Collections.<String,Object>singletonMap("message", "success");
        }else{
            return CollectionUtil.hashMap("message", "failed to save some of the expend types.",
                                          "failedIds", failed);
        }
    }
    
    @RequestMapping(value="/rest/getBranchExpendSubjects")
    public @ResponseBody List<BranchExpendSubject> getBranchExpendSubjects(@RequestParam("mer_id") String merchantId,
                                                                           @RequestParam(value="branch_id", defaultValue="%") String branchId,
                                                                           @RequestParam("term_id") String terminalId,
                                                                           HttpServletRequest request,
                                                                           HttpServletResponse response)
    {
        return this.coreService.findBranchExpendSubjects(merchantId, branchId, terminalId);
    }

    @RequestMapping(value="/rest/saveBranchExpends")
    public @ResponseBody Map<String, Object> saveBranchExpends(@RequestBody BranchExpends expends,
                                                               HttpServletRequest request,
                                                               HttpServletResponse response)
    {
        List<BranchExpend> l = expends.getExpends();
        for(BranchExpend type: l){
            type.setMerchantId(expends.getMerchantId());
            type.setBranchId(expends.getBranchId());
            type.setTerminalId(expends.getTerminalId());
        }
        List<String> failed = this.coreService.saveBranchExpends(l);
        if (failed.isEmpty()){
            return Collections.<String,Object>singletonMap("message", "success");
        }else{
            return CollectionUtil.hashMap("message", "failed to save some of the expend types.",
                                          "failedIds", failed);
        }
    }
    
    @RequestMapping(value="/rest/getBranchExpends")
    public @ResponseBody List<BranchExpend> getBranchExpends(@RequestParam("mer_id") String merchantId,
                                                             @RequestParam(value="branch_id", defaultValue="%") String branchId,
                                                             @RequestParam("term_id") String terminalId,
                                                             HttpServletRequest request,
                                                             HttpServletResponse response)
    {
        return this.coreService.findBranchExpends(merchantId, branchId, terminalId, null, null);
    }
    
    
    
    
    @RequestMapping(value="/rest/findBranchExpends")
    public @ResponseBody Map<String, Object> findBranchExpends(@RequestParam("mer_id") String merchantId,
                                                               @RequestParam("branch_id") String branchId,
                                                               @RequestParam(value="term_id", defaultValue="%") String terminalId,
                                                               @RequestParam("date_start") String startDate,
                                                               @RequestParam("date_end") String endDate,
                                                               HttpServletRequest request,
                                                               HttpServletResponse response)
    {
        return this.queryService.findBranchExpends(merchantId, branchId, terminalId, startDate, endDate);
    }

}
